package com.tqjc.business.interactivewithscan.uils;

import com.tqjc.business.interactivewithscan.consts.CoordConstant;
import com.tqjc.business.interactivewithscan.consts.DeviceEnum;
import com.tqjc.business.interactivewithscan.consts.ExceptionEnum;
import com.tqjc.business.interactivewithscan.entity.*;
import com.tqjc.business.interactivewithscan.exception.CustomException;
import lombok.Data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: C20005_Xianggang_new
 * @description: 库区信息处理工具来
 * @author: Patienyon
 * @create: 2023-11-06 11:23
 **/
public class CoordinatesUtils {

//    private static Logger logger = LoggerFactory.getLogger(CoordinatesUtils.class);


    /**
     * @Description:
     * @Param: [tiancheCode, areaInfo]
     * 废钢1号天车100001  废钢2号:100002
     * 废渣1号天车200001  废渣2号:200002
     * @return: com.tqjc.business.interactivewithscan.entity.LiftingPoint
     * @Author: Patienyon
     * @Date: 2023/11/7
     */
//    public static LiftingPoint getPoints(String craneCode, AreaInfo areaInfo) {
////        KuquDianweiAllDevice dianweiDevice = null;
//            try {
//                if (craneCode.startsWith("200")) {
//                    Map<String, LiftingPoint> points = SpringUtils.getBean(SteelGrabPointThread.class).getPoints(craneCode);
//                    return points.get(areaInfo.getAreaNum());
//            }
//            if (craneCode.startsWith("100")) {
//                Map<String, LiftingPoint> points = SpringUtils.getBean(SlagGrabPointThread.class).getPoints(craneCode);
//                return points.get(areaInfo.getAreaNum());
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if (!EmptyUtil.isEmpty(dianweiDevice)) {
//            Map<String, LiftingPoint> points = dianweiDevice.getPoints(tiancheCode);
//            return points.get(whl.getArea());
//        }
////        KuquDianweiDevice dianwei = null;
////        try {
////            if (DeviceEnum.Tianche_Slag_1.getDeviceCode().equals(tiancheCode)) {
////                dianwei = DeviceThreadPool.devices.getObjectNull(DeviceEnum.Kuqu_Dianwei_Slag_One.getDeviceCode(), KuquDianweiDevice.class);
////            }
////            if (DeviceEnum.Tianche_Slag_2.getDeviceCode().equals(tiancheCode)) {
////                dianwei = DeviceThreadPool.devices.getObjectNull(DeviceEnum.Kuqu_Dianwei_Slag_Two.getDeviceCode(), KuquDianweiDevice.class);
////            }
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
////        if (!EmptyUtil.isEmpty(dianwei)) {
////            return dianwei.points.get(whl.getArea());
////        }
//        return null;
//    }

    /**
     * 库区扫描数据处理
     *
     * @param scanning 扫描数据
     * @return 库区所有点位信息
     */
    public static List<Coord> coordinatesScanningHandle(String scanning, Integer type) {
        if (EmptyUtil.isEmpty(scanning)) {
            throw new CustomException(ExceptionEnum.SCANNER_FORMAT_ERROR);
        }
//        System.out.println(scanning);
        //获取第一个数据
        Split split = splitScanning(scanning, 0);
        if (split.index == -1 || !"fb".equals(split.result)) {
            throw new CustomException(ExceptionEnum.SCANNER_FORMAT_ERROR);
        }
        //获取第二个数据
        split = splitScanning(scanning, split.index + 1);
        if (split.index == -1 || !("FGResult".equals(split.result) || "FZResult".equals(split.result))) {
            throw new CustomException(ExceptionEnum.SCANNER_FORMAT_ERROR);
        }
        //获取第三个数据
        split = splitScanning(scanning, split.index + 1);
        if (split.index == -1) {
            throw new CustomException(ExceptionEnum.SCANNER_FORMAT_ERROR);
        }
        //获取第四个数据
        split = splitScanning(scanning, split.index + 1);
        if (split.index == -1) {
            throw new CustomException(ExceptionEnum.SCANNER_FORMAT_ERROR);
        }
        //获取第五个数据
        split = splitScanning(scanning, split.index + 1);
        if (split.index == -1) {
            throw new CustomException(ExceptionEnum.SCANNER_FORMAT_ERROR);
        }
        //获取第六个数据
        split = splitScanning(scanning, split.index + 1);
        if (split.index == -1) {
            throw new CustomException(ExceptionEnum.SCANNER_FORMAT_ERROR);
        }
        int length = Integer.parseInt(split.result);
        //获取第七个数据
        split = splitScanning(scanning, split.index + 1);
        if (split.index == -1 || split.result.length() != length) {
            throw new CustomException(ExceptionEnum.SCANNER_FORMAT_ERROR);
        }
        String result = split.result;
        if (EmptyUtil.isEmpty(result)) {
            throw new CustomException(ExceptionEnum.SCANNER_NO_DEM);
        }
        //获取第八个数据
        split = splitScanning(scanning, split.index + 1);
        if (split.index != -1) {
//            throw new CustomException(ExceptionEnum.SCANNER_FORMAT_ERROR);
        }
        String[] resultArrays = result.split(";");
        if ((resultArrays.length != 950 && type == 2) || (type == 1 && resultArrays.length != 950)) {
            throw new CustomException(ExceptionEnum.SCANNER_DEM_ERROR);
        }
        List<Coord> coordList = new ArrayList<>();
        int start = type.equals(2) ? 0 : type.equals(1) ? 24140 : -1;
        if (start == -1) {
            return coordList;
        }
        for (int j = 0; j < 950; j++) {
            int x = j * 200 + 100;
            String[] highs = resultArrays[j].split(",");
            if (highs.length != 90) {
                throw new CustomException(ExceptionEnum.SCANNER_DEM_ERROR);
            }
            for (int i = 0; i < 90; i++) {
                int z = Integer.parseInt(highs[i]);
                int y = start + i * 200 + 100;
                Coord coord = new Coord(x, y, z);
                coordList.add(coord);
            }
        }
        return coordList;
    }


    public static List<Coord> yuntaiScanningHandle(String scanning) {
        List<Coord> coordList = new ArrayList<>();
        String[] ss = scanning.split(" ");
        if (!scanning.startsWith("fb TruckDEM ")) {
            throw new CustomException(ExceptionEnum.SCANNER_DEM_ERROR);
        }
        int length = Integer.parseInt(ss[2]);
        if (length <= 0) {
            throw new CustomException(ExceptionEnum.SCANNER_NO_DEM);
        }

        String[] cs = ss[3].split(";");
        for (String c : cs) {
            Coord coord = new Coord(c);

            coordList.add(coord);
        }
        return coordList;
    }

    private static Split splitScanning(String scanning, Integer index) {
        int i = scanning.indexOf(" ", index);
        Split split = new Split();
        String s = scanning.substring(index, i == -1 ? scanning.length() : i);
        split.setIndex(i);
        split.setResult(s);
        return split;
    }
//
//    /**
//     * 校验点位是否和天车同跨
//     *
//     * @param coord      点位信息
//     * @param deviceInfo 天车编码
//     * @return
//     */
//    public static boolean checkCoord(Coord coord, UfcDeviceInfo deviceInfo, String deviceCode) {
//        if (deviceCode.startsWith("100") && deviceInfo.getCoordForY() - coord.getY() < 0) {
//            return true;
//        }
//        if (deviceCode.startsWith("200") && deviceInfo.getCoordForY() - coord.getY() > 0) {
//            return true;
//        }
//        return false;
//    }
//

    /**
     * 获取卸料点
     *
     * @param areaCoords  放料区域
     * @param tiancheInfo 天车信息
     * @param material    所放物料主键
     * @param areaType    所放区域类型 true：库区；false：槽车
     * @return
     */

    public static Coord dischargeCoord(List<Coord> areaCoords, QuadrilateralVertex vertex, Integer length, Integer width, boolean areaType) {
        int dischargeType = 2;
//        if (!areaType) {
//            dischargeType = material.getDischargeType();
//        }
//        UfcDeviceInfo thisDevice = tiancheInfo.getThisDevice();
//        if (thisOld != null) {
//            QuadrilateralVertex cp = queryVertex(thisOld, thisDevice.getLength() / 2, thisDevice.getWidth() / 2);
//            removeDesignatedAreas(cp, areaCoords, true);
//        }
//        if (otherOld != null) {
//            UfcDeviceInfo other = tiancheInfo.getOtherDevice();
//            QuadrilateralVertex cp = queryVertex(otherOld, other.getLength() / 2, other.getWidth() / 2);
//            removeDesignatedAreas(cp, areaCoords, true);
//        }
        vertex.setSafeDistance(0);
        int max = vertex.getMaxX();
        int mix = vertex.getMinX();
        int may = vertex.getMaxY();
        int miy = vertex.getMinY();
        int maz = areaType ? CoordConstant.Maximum_Height_To_Top : CoordConstant.Tanker_Safe_Height;
        int miz = areaType ? CoordConstant.Maximum_Height_To_Ground : 13500;
        int scope = 500;
        for (int z = miz; z > maz; z = z - scope) {
            // xy 从小到达
            if (dischargeType == 1) {
                for (int y = miy; y + width <= may; y = y + 100) {
                    for (int x = mix; x + length <= max; x = x + 100) {
                        Coord coord = new Coord(x + length / 2, y + width / 2, 0);
                        QuadrilateralVertex sc = queryVertex(coord, length / 2, width / 2);
                        List<Coord> screens = screenDesignationRegion(sc, areaCoords);
                        Coord t = screens.stream().min(new CoordSort(4)).orElse(null);
                        if (t == null) {
                            continue;
//                            throw new CustomException(ExceptionEnum.NOT_DATA, "查找放料点失败：获取吸盘区域错误");
                        }
                        if (t.getZ() < z - scope) {
                            continue;
                        }
                        coord.setZ(t.getZ());
                        return coord;
                    }
                }
            }
            // xy 从大到小
            if (dischargeType == 2) {
                for (int y = may; y - width >= miy; y = y - 100) {
                    for (int x = max; x - length >= mix; x = x - 100) {
                        Coord coord = new Coord(x - length / 2, y - width / 2, 0);
                        QuadrilateralVertex sc = queryVertex(coord, length / 2, width / 2);
                        List<Coord> screens = screenDesignationRegion(sc, areaCoords);
                        Coord t = screens.stream().min(new CoordSort(4)).orElse(null);
                        if (t == null) {
                            continue;
                        }
                        if (t.getZ() < z - scope) {
                            continue;
                        }
                        coord.setZ(t.getZ());
                        return coord;
                    }
                }
            }
        }
        throw new CustomException(ExceptionEnum.NOT_DATA, "查找放料点失败");
    }
//
//    /**
//     * 获取库区卸料点
//     *
//     * @param areaCoords  吸料区域
//     * @param vertex      吸料区域顶点
//     * @param tiancheInfo 天车信息
//     * @param material    所吸物料主键
//     * @param thisOld     第一个放料点
//     * @param otherOld    第一个放料点
//     * @return
//     */
//    public static Coord grabAreaCoord(List<Coord> areaCoords, QuadrilateralVertex vertex, SameSpanTianche tiancheInfo, BMaterialBaseInfo material, Coord thisOld, Coord otherOld) {
//        UfcDeviceInfo thisDevice = tiancheInfo.getThisDevice();
//        if (thisOld != null) {
//            QuadrilateralVertex cp = queryVertex(thisOld, thisDevice.getLength() / 2, thisDevice.getWidth() / 2);
//            removeDesignatedAreas(cp, areaCoords, false);
//        }
//        if (otherOld != null) {
//            UfcDeviceInfo other = tiancheInfo.getOtherDevice();
//            QuadrilateralVertex cp = queryVertex(otherOld, other.getLength() / 2, other.getWidth() / 2);
//            removeDesignatedAreas(cp, areaCoords, false);
//        }
//        vertex.setSafeDistance(0);
//        int max = vertex.getMaxX();
//        int mix = vertex.getMinX();
//        int may = vertex.getMaxY();
//        int miy = vertex.getMinY();
//        int maz = areaCoords.stream().min(new CoordSort(4)).orElse(new Coord()).getZ();
//        int miz = areaCoords.stream().max(new CoordSort(4)).orElse(new Coord()).getZ();
//        int incrForZ = material.getLiftingHeight() == null ? 1 : material.getLiftingHeight();
//        int scope = 60;
//        for (int z = maz; z + incrForZ <= miz; z = z + incrForZ) {
//            //1：xy 从小到达
//            if (material.getGrabType().equals(1)) {
//                for (int y = miy; y + thisDevice.getWidth() <= may; y = y + thisDevice.getWidth() / 4) {
//                    for (int x = mix; x + thisDevice.getLength() <= max; x = x + thisDevice.getLength() / 4) {
//                        Coord coord = new Coord(x + thisDevice.getLength() / 2, y + thisDevice.getWidth() / 2, 0);
//                        QuadrilateralVertex sv = queryVertex(coord, thisDevice.getLength() / 2, thisDevice.getWidth() / 2);
//                        List<Coord> screens = screenDesignationRegion(sv, areaCoords);
//                        int cz = z;
//                        List<Coord> ts = screens.stream().filter(f -> f.getZ() <= cz + incrForZ).collect(Collectors.toList());
//                        if (ts.size() < scope) {
//                            continue;
//                        }
//                        Coord grab = checkGrabScreens(screens, thisDevice, vertex, z, incrForZ, 1);
//                        if (grab == null) {
//                            continue;
//                        }
//                        return grab;
//                    }
//                }
//            }
//            //2：xy 从大到小
//            if (material.getGrabType().equals(2)) {
//                for (int y = may; y - thisDevice.getWidth() >= miy; y = y - thisDevice.getWidth() / 4) {
//                    for (int x = max; x - thisDevice.getLength() >= mix; x = x - thisDevice.getLength() / 4) {
//                        Coord coord = new Coord(x - thisDevice.getLength() / 2, y - thisDevice.getWidth() / 2, 0);
//                        QuadrilateralVertex sv = queryVertex(coord, thisDevice.getLength() / 2, thisDevice.getWidth() / 2);
//                        List<Coord> screens = screenDesignationRegion(sv, areaCoords);
//                        int cz = z;
//                        List<Coord> ts = screens.stream().filter(f -> f.getZ() <= cz + incrForZ).collect(Collectors.toList());
//                        if (ts.size() < scope) {
//                            continue;
//                        }
//                        Coord grab = checkGrabScreens(screens, thisDevice, vertex, z, incrForZ, 2);
//                        if (grab == null) {
//                            continue;
//                        }
//                        return grab;
//                    }
//                }
//            }
//            //3：X：从小到大 Y：从大到小 （不考虑高度问题）
//            if (material.getGrabType().equals(3)) {
//                for (int y = may; y - thisDevice.getWidth() >= miy; y = y - thisDevice.getWidth() / 4) {
//                    for (int x = mix; x + thisDevice.getLength() <= max; x = x + thisDevice.getLength() / 4) {
//                        Coord coord = new Coord(x + thisDevice.getLength() / 2, y - thisDevice.getWidth() / 2, 0);
//                        QuadrilateralVertex sv = queryVertex(coord, thisDevice.getLength() / 2, thisDevice.getWidth() / 2);
//                        List<Coord> screens = screenDesignationRegion(sv, areaCoords);
//                        int cz = CoordConstant.Maximum_Height_To_Ground - incrForZ;
//                        List<Coord> ts = screens.stream().filter(f -> f.getZ() <= cz + incrForZ).collect(Collectors.toList());
//                        if (ts.size() < scope) {
//                            continue;
//                        }
//                        Coord grab = checkGrabScreens(screens, thisDevice, vertex, cz, incrForZ, 3);
//                        if (grab == null) {
//                            continue;
//                        }
//                        return grab;
//                    }
//                }
//            }
//        }
//        return null;
//    }
//
//    /**
//     * 校验获取货车吸点
//     *
//     * @param screens
//     * @param tiancheInfo
//     * @param z
//     * @return
//     */
//    private static Coord checkGrabScreens(List<Coord> screens, UfcDeviceInfo tiancheInfo, QuadrilateralVertex vertex, int z) {
//        int siy = vertex.getMinY();
//        Iterator<Coord> iterator = screens.iterator();
//        while (iterator.hasNext()) {
//            Coord rangeCoord = iterator.next();
//            int tz = rangeCoord.getZ() + (int) ((rangeCoord.getY() - siy) * tiancheInfo.getSlope());
//            if (tz > z) {
//                iterator.remove();
//            }
//        }
//        if (screens.size() <= 0) {
//            return null;
//        }
//        int max = screens.stream().max(new CoordSort(2)).get().getX();
//        int mix = screens.stream().min(new CoordSort(2)).get().getX();
//        int may = screens.stream().max(new CoordSort(3)).get().getY();
//        int miy = screens.stream().min(new CoordSort(3)).get().getY();
////        Coord determine = new Coord((max + mix) / 2, (may + miy) / 2, z);
//        Coord determine = new Coord(mix + tiancheInfo.getLength() / 2, may - tiancheInfo.getWidth() / 2, z);
//        // 校验位置是否超出大的范围
//        if (vertex.getMinX() > determine.getX() - tiancheInfo.getLength() / 2) {
//            determine.setX(vertex.getMinX() + tiancheInfo.getLength() / 2);
//        }
//        if (vertex.getMinY() > determine.getY() - tiancheInfo.getWidth() / 2) {
//            determine.setY(vertex.getMinY() + tiancheInfo.getWidth() / 2);
//        }
//        if (vertex.getMaxX() < determine.getX() + tiancheInfo.getLength() / 2) {
//            determine.setX(vertex.getMaxX() - tiancheInfo.getLength() / 2);
//        }
//        if (vertex.getMaxY() < determine.getY() + tiancheInfo.getWidth() / 2) {
//            determine.setY(vertex.getMaxY() - tiancheInfo.getWidth() / 2);
//        }
//        int tz = screens.stream().min(new CoordSort(4)).get().getZ();
//        determine.setZ(tz);
//        return determine;
//    }
//

    /**
     * 去除指定区域
     *
     * @param vertex    小区域范围
     * @param coordList 大区域点位
     * @param scope     删除高度
     */
    public static void removeDesignatedAreas(QuadrilateralVertex vertex, List<Coord> coordList, Integer scope) {
        /*排除掉不在指定区域的坐标*/
        Iterator<Coord> iterator = coordList.iterator();
        while (iterator.hasNext()) {
            Coord rangeCoord = iterator.next();
            if (pointInsideRectangle(vertex, rangeCoord)) {
                rangeCoord.setZ(rangeCoord.getZ() + scope);
            }
        }
    }

    public static Coord grabTruckCoord(List<Coord> areaCoords, QuadrilateralVertex vertex, SameSpanTianche tiancheInfo, Coord otherOld) {
        UfcDeviceInfo thisDevice = tiancheInfo.getThisDevice();
        int scope = 1000;
        if (otherOld != null) {
            UfcDeviceInfo other = tiancheInfo.getOtherDevice();
            QuadrilateralVertex cp = queryVertex(otherOld, other.getLength() / 2, other.getWidth() / 2);
            removeDesignatedAreas(cp, areaCoords, scope);
        }
        vertex.setSafeDistance(0);
        int max = vertex.getMaxX();
        int mix = vertex.getMinX();
        int may = vertex.getMaxY();
        int miy = vertex.getMinY();
        int incrForZ = 200;
        Coord maxC = areaCoords.stream().min(new CoordSort(4)).get();
        Coord minC = areaCoords.stream().max(new CoordSort(4)).get();
        int maz = maxC.getZ() + (int) ((maxC.getY() - miy) * thisDevice.getSlope());
//        int miz = minC.getZ() + (int) ((minC.getY() - miy) * thisDevice.getSlope());
        int miz = CoordConstant.Car_Safe_Height;
        for (int z = maz; z < miz; z = z + 700) {
            for (int y = may; y - thisDevice.getWidth() > miy; y = y - thisDevice.getWidth()) {
                for (int x = mix; x + thisDevice.getLength() < max; x = x + thisDevice.getLength()) {
                    Coord coord = new Coord(x + thisDevice.getLength() / 2, y - thisDevice.getWidth() / 2, 0);
                    QuadrilateralVertex sv = queryVertex(coord, thisDevice.getLength() / 2, thisDevice.getWidth() / 2);
                    List<Coord> screens = screenDesignationRegion(sv, areaCoords);
                    Coord tc = screens.stream().min(new CoordSort(4)).get();
                    int mz = tc.getZ() + (int) (thisDevice.getSlope() * (tc.getY() - miy));
                    // 吸盘区域最高点就是在车厢安全高度以下或者接近
                    if (mz >= CoordConstant.Car_Safe_Height - incrForZ || mz > z) {
                        continue;
                    }
                    Coord grab = checkGrabScreens(screens, thisDevice, vertex, z + incrForZ);
                    if (grab == null) {
                        continue;
                    }
                    grab.setTestX(tc.getX());
                    grab.setTestY(tc.getY());
                    grab.setTestZ(tc.getZ());
                    return grab;
                }
            }
        }
        return null;
    }

    /**
     * 校验获取货车吸点
     *
     * @param screens
     * @param tiancheInfo
     * @param z
     * @return
     */
    private static Coord checkGrabScreens(List<Coord> screens, UfcDeviceInfo tiancheInfo, QuadrilateralVertex vertex, int z) {
        int siy = vertex.getMinY();
        Iterator<Coord> iterator = screens.iterator();
        while (iterator.hasNext()) {
            Coord rangeCoord = iterator.next();
            int tz = rangeCoord.getZ() + (int) ((rangeCoord.getY() - siy) * tiancheInfo.getSlope());
            if (tz > z) {
                iterator.remove();
            }
        }
        if (screens.size() <= 0) {
            return null;
        }
        int max = screens.stream().max(new CoordSort(2)).get().getX();
        int mix = screens.stream().min(new CoordSort(2)).get().getX();
        int may = screens.stream().max(new CoordSort(3)).get().getY();
        int miy = screens.stream().min(new CoordSort(3)).get().getY();
//        Coord determine = new Coord((max + mix) / 2, (may + miy) / 2, z);
        Coord determine = new Coord(mix + tiancheInfo.getLength() / 2, may - tiancheInfo.getWidth() / 2, z);
        // 校验位置是否超出大的范围
        if (vertex.getMinX() > determine.getX() - tiancheInfo.getLength() / 2) {
            determine.setX(vertex.getMinX() + tiancheInfo.getLength() / 2);
        }
        if (vertex.getMinY() > determine.getY() - tiancheInfo.getWidth() / 2) {
            determine.setY(vertex.getMinY() + tiancheInfo.getWidth() / 2);
        }
        if (vertex.getMaxX() < determine.getX() + tiancheInfo.getLength() / 2) {
            determine.setX(vertex.getMaxX() - tiancheInfo.getLength() / 2);
        }
        if (vertex.getMaxY() < determine.getY() + tiancheInfo.getWidth() / 2) {
            determine.setY(vertex.getMaxY() - tiancheInfo.getWidth() / 2);
        }
        int tz = screens.stream().min(new CoordSort(4)).get().getZ();
        determine.setZ(tz);
        return determine;
    }
//

    /**
     * 检查吸料区域，并更新吸料坐标
     *
     * @param screens     吸料区域
     * @param tiancheInfo 天车信息
     * @param vertex      待吸料大区域
     * @return
     */
    public static Coord checkGrabScreens(List<Coord> screens, UfcDeviceInfo tiancheInfo, QuadrilateralVertex vertex, int z, int incrForZ, int type) {
        int miy = screens.stream().min(new CoordSort(3)).orElse(new Coord()).getY();
        int may = screens.stream().max(new CoordSort(3)).orElse(new Coord()).getY();
        int mix = screens.stream().min(new CoordSort(2)).orElse(new Coord()).getX();
        int max = screens.stream().max(new CoordSort(2)).orElse(new Coord()).getX();
        int incrForX = (max - mix) / 10;
        int incrForY = (may - miy) / 10;
        int determineMinX = mix;
        int determineMaxX = max;
        int determineMinY = miy;
        int determineMaxY = may;
        int s = 10;
        boolean is = false;
        for (int x = mix + incrForX; x < max - incrForX; x = x + incrForX) {
            Coord coord = new Coord((x + x + incrForX) / 2, miy + (may + miy) / 2, 0);
            QuadrilateralVertex sv = queryVertex(coord, incrForX / 2, (may + miy) / 2);
            List<Coord> sc = screenDesignationRegion(sv, screens);
            List<Coord> ts = sc.stream().filter(f -> f.getZ() <= z + incrForZ).collect(Collectors.toList());
            if (ts.size() > s) {
                determineMinX = x;
                is = true;
                break;
            }
        }
        for (int y = miy + incrForY; y < may - incrForY; y = y + incrForY) {
            Coord coord = new Coord(mix + (miy + max) / 2, (y + y + incrForY) / 2, 0);
            QuadrilateralVertex sv = queryVertex(coord, (miy + max) / 2, incrForY / 2);
            List<Coord> sc = screenDesignationRegion(sv, screens);
            List<Coord> ts = sc.stream().filter(f -> f.getZ() <= z + incrForZ).collect(Collectors.toList());
            if (ts.size() > s) {
                determineMinY = y;
                is = true;
                break;
            }
        }
        for (int x = max - incrForX; x > mix + incrForX; x = x - incrForX) {
            Coord coord = new Coord((x + x - incrForX) / 2, miy + (may + miy) / 2, 0);
            QuadrilateralVertex sv = queryVertex(coord, incrForX / 2, (may + miy) / 2);
            List<Coord> sc = screenDesignationRegion(sv, screens);
            List<Coord> ts = sc.stream().filter(f -> f.getZ() <= z + incrForZ).collect(Collectors.toList());
            if (ts.size() > s) {
                determineMaxX = x;
                is = true;
                break;
            }
        }
        for (int y = may - incrForY; y > mix + incrForY; y = y - incrForY) {
            Coord coord = new Coord(mix + (miy + max) / 2, (y + y - incrForY) / 2, 0);
            QuadrilateralVertex sv = queryVertex(coord, (miy + max) / 2, incrForY / 2);
            List<Coord> sc = screenDesignationRegion(sv, screens);
            List<Coord> ts = sc.stream().filter(f -> f.getZ() <= z + incrForZ).collect(Collectors.toList());
            if (ts.size() > s) {
                determineMaxY = y;
                is = true;
                break;
            }
        }
        if (!is) {
            return null;
        }
        Coord determine = new Coord((determineMaxX + determineMinX) / 2, (determineMaxY + determineMinY) / 2, z);
        if (type == 1) {
            //1：xy 从小到达
            determine = new Coord(determineMinX + tiancheInfo.getLength() / 2, determineMinY + tiancheInfo.getWidth() / 2, z);
        }
        if (type == 2) {
            //2：xy 从大到小
            determine = new Coord(determineMaxX - tiancheInfo.getLength() / 2, determineMaxY - tiancheInfo.getLength() / 2, z);
        }
        if (type == 3) {
            //3：X：从小到大 Y：从大到小 （不考虑高度问题）
            determine = new Coord(determineMinX + tiancheInfo.getLength() / 2, determineMaxY - tiancheInfo.getLength() / 2, z);
        }
        // 校验位置是否超出大的范围
        if (vertex.getMinX() > determine.getX() - tiancheInfo.getLength() / 2) {
            determine.setX(vertex.getMinX() + tiancheInfo.getLength() / 2);
        }
        if (vertex.getMinY() > determine.getY() - tiancheInfo.getWidth() / 2) {
            determine.setY(vertex.getMinY() + tiancheInfo.getWidth() / 2);
        }
        if (vertex.getMaxX() < determine.getX() + tiancheInfo.getLength() / 2) {
            determine.setX(vertex.getMaxX() - tiancheInfo.getLength() / 2);
        }
        if (vertex.getMaxY() < determine.getY() + tiancheInfo.getWidth() / 2) {
            determine.setY(vertex.getMaxY() - tiancheInfo.getWidth() / 2);
        }
        Coord t = screens.stream().min(new CoordSort(4)).get();
        int h = t.getZ();
        determine.setZ(h);
        determine.setTestZ(h);
        determine.setTestY(t.getY());
        determine.setTestX(t.getX());
        return determine;
    }

    //
//    /**
//     * 去除指定区域
//     *
//     * @param vertex    小区域范围
//     * @param coordList 大区域点位
//     * @param scope     删除高度
//     */
//    public static void removeDesignatedAreas(QuadrilateralVertex vertex, List<Coord> coordList, Integer scope) {
//        /*排除掉不在指定区域的坐标*/
//        Iterator<Coord> iterator = coordList.iterator();
//        while (iterator.hasNext()) {
//            Coord rangeCoord = iterator.next();
//            if (pointInsideRectangle(vertex, rangeCoord)) {
//                rangeCoord.setZ(rangeCoord.getZ() + scope);
//            }
//        }
//    }
//
    public static void removeDesignatedAreas(QuadrilateralVertex vertex, List<Coord> coordList, boolean type) {
        int z = type ? CoordConstant.Safe_High_Height : CoordConstant.Safe_Low_Height;
        /*排除掉不在指定区域的坐标*/
        Iterator<Coord> iterator = coordList.iterator();
        while (iterator.hasNext()) {
            Coord rangeCoord = iterator.next();
            if (pointInsideRectangle(vertex, rangeCoord)) {
                rangeCoord.setZ(z);
            }
        }
    }

    public static QuadrilateralVertex queryVertex(Coord coord, int x, int y) {
        List<Coord> cps = new ArrayList();
        int x1 = coord.getX() + x;
        int x2 = coord.getX() - x;
        int y1 = coord.getY() + y;
        int y2 = coord.getY() - y;
        cps.add(new Coord(x1, y1, 0));
        cps.add(new Coord(x1, y2, 0));
        cps.add(new Coord(x2, y1, 0));
        cps.add(new Coord(x2, y2, 0));
        QuadrilateralVertex cp = changeVertex(cps, 0);
        return cp;
    }

    /**
     * 查询槽车四个顶点区域
     *
     * @param scanningArea
     * @param tankerCode
     * @return
     */
    public static List<Coord> queryArea(String scanningArea, String tankerCode) {
        return queryArea(scanningArea, tankerCode, null);
    }

    public static List<Coord> queryArea(String scanningArea, String tankerCode, Date queryDate) {
//        System.out.println("当前扫描区域为"+ scanningArea);
        List<Coord> coordList = new ArrayList<>();
        String[] ss = scanningArea.split(" ");
        if (!scanningArea.startsWith("fb ScanResult ")) {
            return null;
        }
        if (queryDate != null) {
            String d = ss[2] + " " + ss[3];
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date date = sf.parse(d);
                if (date.getTime() - queryDate.getTime() > 80000) {
                    throw new CustomException(ExceptionEnum.SCANNER_DATE_ERROR);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        String[] cs = ss[6].split(";");
        for (String c : cs) {
            String[] split = c.split(",");
            Coord coord = new Coord();
            for (String s : split) {
                String[] index = s.split(":");
                if (index.length != 2) {
                    continue;
                }
                if (index[0].endsWith("X")) {
                    coord.setX(Integer.parseInt(index[1]));
                }
                if (index[0].endsWith("Y")) {
                    coord.setY(Integer.parseInt(index[1]));
                }
                if (index[0].endsWith("Z")) {
                    coord.setZ(Integer.parseInt(index[1]));
                }
            }
            coordList.add(coord);
        }
        System.out.println("当前槽车为:" + tankerCode);
        if (DeviceEnum.Tanker_Double_Track_1.getDeviceCode().equals(tankerCode)
                || DeviceEnum.Scanning_Steel.getDeviceCode().equals(tankerCode) || DeviceEnum.Scanning_Slag.getDeviceCode().equals(tankerCode)) {
            coordList = coordList.subList(0, 4);
            if (!checkCoords(coordList)) {
                throw new CustomException(ExceptionEnum.SCANNER_AREA_ERROR);
            }
            return coordList;
        } else if (DeviceEnum.Tanker_Double_Track_2.getDeviceCode().equals(tankerCode)) {
            coordList = coordList.subList(4, 8);
            if (!checkCoords(coordList)) {
                throw new CustomException(ExceptionEnum.SCANNER_AREA_ERROR);
            }
            return coordList;
        } else if (DeviceEnum.Tanker_Wide_Track_3.getDeviceCode().equals(tankerCode)) {
            if (!checkCoords(coordList)) {
                throw new CustomException(ExceptionEnum.SCANNER_AREA_ERROR);
            }
            return coordList;
        }
        throw new CustomException(ExceptionEnum.SCANNER_DEVICE_ERROR);
    }

    private static Boolean checkCoords(List<Coord> coords) {
        if (EmptyUtil.isEmpty(coords)) {
            throw new CustomException(ExceptionEnum.SCANNER_DEVICE_ERROR);
        }
        Coord t = new Coord();
        for (Coord coord : coords) {
            if (t.equals(coord)) {
                return false;
            }
        }
        return true;
    }

    //
//    public static Coord dischargeAreaCoord(List<Coord> region, QuadrilateralVertex vertex, SameSpanTianche tiancheInfo, BMaterialBaseInfo material, Coord dischargeThisOld, Coord dischargeOtherOld) {
//        UfcDeviceInfo thisDevice = tiancheInfo.getThisDevice();
//        if (dischargeThisOld != null) {
//            QuadrilateralVertex cp = queryVertex(dischargeThisOld, thisDevice.getLength() / 2, thisDevice.getWidth() / 2);
//            removeDesignatedAreas(cp, region, true);
//        }
//        if (dischargeOtherOld != null) {
//            UfcDeviceInfo other = tiancheInfo.getOtherDevice();
//            QuadrilateralVertex cp = queryVertex(dischargeOtherOld, other.getLength() / 2, other.getWidth() / 2);
//            removeDesignatedAreas(cp, region, true);
//        }
//        vertex.setSafeDistance(0);
//        int max = vertex.getMaxX();
//        int mix = vertex.getMinX();
//        int may = vertex.getMaxY();
//        int miy = vertex.getMinY();
//        int maz = region.stream().min(new CoordSort(4)).orElse(new Coord()).getZ();
//        int miz = region.stream().max(new CoordSort(4)).orElse(new Coord()).getZ();
//        int incrForZ = material.getLiftingHeight() == null ? 100 : material.getLiftingHeight();
//        int scope = 500;
//        for (int z = miz; z - scope > CoordConstant.Safe_High_Height; z = z - scope) {
//            for (int y = miy; y + thisDevice.getWidth() <= may; y = y + 100) {
//                for (int x = mix; x + thisDevice.getLength() <= max; x = x + 100) {
//                    Coord coord = new Coord(x + thisDevice.getLength() / 2, y + thisDevice.getWidth() / 2, 0);
//                    QuadrilateralVertex sc = queryVertex(coord, thisDevice.getLength() / 2, thisDevice.getWidth() / 2);
//                    List<Coord> screens = screenDesignationRegion(sc, region);
//                    Coord t = screens.stream().min(new CoordSort(4)).orElse(null);
//                    if (t == null) {
//                        throw new CustomException(ExceptionEnum.NOT_DATA, "查找放料点失败：获取吸盘区域错误");
//                    }
//                    if (t.getZ() < z - scope) {
//                        List<Coord> temps = queryRangeHeight(screens, z - scope, z);
//                        int x1 = temps.stream().min(new CoordSort(2)).orElse(new Coord()).getX();
//                        if (x1 > 0) {
//                            x = x1;
//                        }
//                        continue;
//                    }
//                    coord.setZ(t.getZ());
//                    return coord;
//                }
//                Coord coord = new Coord((max + mix) / 2, y + thisDevice.getWidth() / 2, 0);
//                QuadrilateralVertex sc = queryVertex(coord, (max + mix) / 2, thisDevice.getWidth() / 2);
//                List<Coord> temps = screenDesignationRegion(sc, region);
//                int y1 = temps.stream().min(new CoordSort(3)).orElse(new Coord()).getY();
//                if (y1 > 0) {
//                    y = y1;
//                }
//            }
//        }
//        return null;
//    }
//
    @Data
    private static class Split {
        private int index;
        private String result;
    }
//
//
//    /**
//     * 给选定区域增加高度
//     *
//     * @param vertex
//     * @param coordList
//     */
//    public static void regionIncreaseHeight(QuadrilateralVertex vertex, List<Coord> coordList, Integer height) {
//        Iterator<Coord> iterator = coordList.iterator();
//        while (iterator.hasNext()) {
//            Coord rangeCoord = iterator.next();
//            if (pointInsideRectangle(vertex, rangeCoord)) {
//                rangeCoord.setZ(rangeCoord.getZ() + height);
//            }
//        }
//    }
//

    /**
     * 筛选指定区域
     *
     * @param vertex
     * @param coordList
     * @return
     */
    public static List<Coord> screenDesignationRegion(QuadrilateralVertex vertex, List<Coord> coordList) {
        List<Coord> region = new ArrayList<>();
        region.addAll(coordList);
        /*排除掉不在指定区域的坐标*/
        Iterator<Coord> iterator = region.iterator();
        while (iterator.hasNext()) {
            Coord rangeCoord = iterator.next();
            if (!pointInsideRectangle(vertex, rangeCoord)) {
                iterator.remove();
            }
        }
        return region;
    }

    /**
     * 去除阴影
     *
     * @param coordList
     */
    public static void removeShadows(List<Coord> coordList) {
        Iterator<Coord> iterator = coordList.iterator();
        while (iterator.hasNext()) {
            Coord rangeCoord = iterator.next();
            if (rangeCoord.getZ() == 0) {
                iterator.remove();
            }
        }
    }

    /**
     * //     * 去除阴影填补
     * //     *
     * //     * @param coordList
     * //
     */
    public static void fillShadows(List<Coord> coordList, int height) {
        Iterator<Coord> iterator = coordList.iterator();
        while (iterator.hasNext()) {
            Coord rangeCoord = iterator.next();
            if (rangeCoord.getZ() == 0) {
                rangeCoord.setZ(height);
            }
        }
    }

    //
//    /**
//     * 去除阴影
//     *
//     * @param coordList
//     */
//    public static void removeShadows(List<Coord> coordList, int height, boolean type) {
//        Iterator<Coord> iterator = coordList.iterator();
//        while (iterator.hasNext()) {
//            Coord rangeCoord = iterator.next();
//            if (type) {
//                if (rangeCoord.getZ() >= height) {
//                    iterator.remove();
//                }
//            } else {
//                if (rangeCoord.getZ() <= height) {
//                    iterator.remove();
//                }
//            }
//        }
//    }
//
    public static List<Coord> queryRangeHeight(List<Coord> coordList, int startHeight, int endHeight) {
        List<Coord> coords = new ArrayList<>();
        Iterator<Coord> iterator = coordList.iterator();
        while (iterator.hasNext()) {
            Coord rangeCoord = iterator.next();
            if (rangeCoord.getZ() >= startHeight && rangeCoord.getZ() <= endHeight) {
                coords.add(rangeCoord);
            }
        }
        return coords;
    }

    /**
     * 对角线转换成 四边形顶点
     *
     * @return
     */
    public static QuadrilateralVertex changeVertex(List<Coord> coords, Integer safeDistance) {
        if (coords == null || coords.size() != 4) {
            return null;
        }
        Integer sy = (coords.get(0).getY() + coords.get(1).getY() + coords.get(2).getY() + coords.get(3).getY()) / 4;
        Integer sx = (coords.get(0).getX() + coords.get(1).getX() + coords.get(2).getX() + coords.get(3).getX()) / 4;

        QuadrilateralVertex vertex = new QuadrilateralVertex();
        List<Coord> collect = coords.stream().filter(c -> c.getY() > sy).collect(Collectors.toList());
        if (collect.size() != 2) {
            List<Coord> right = coords.stream().filter(c -> c.getX() < sx).collect(Collectors.toList());
            if (right.size() != 2) {
                return null;
            }
            if (right.get(0).getY() > right.get(1).getY()) {
                vertex.setRightUpper(right.get(0));
                vertex.setRightLower(right.get(1));
            } else {
                vertex.setRightUpper(right.get(1));
                vertex.setRightLower(right.get(0));
            }
            List<Coord> left = coords.stream().filter(c -> c.getX() > sx).collect(Collectors.toList());
            if (left.size() != 2) {
                return null;
            }
            if (left.get(0).getY() > left.get(1).getY()) {
                vertex.setLeftUpper(left.get(0));
                vertex.setLeftLower(left.get(1));
            } else {
                vertex.setLeftUpper(left.get(1));
                vertex.setLeftLower(left.get(0));
            }
        } else {
            List<Coord> upper = coords.stream().filter(c -> c.getY() > sy).collect(Collectors.toList());
            if (upper.size() != 2) {
                return null;
            }
            if (upper.get(0).getX() < upper.get(1).getX()) {
                vertex.setRightUpper(upper.get(0));
                vertex.setLeftUpper(upper.get(1));
            } else {
                vertex.setRightUpper(upper.get(1));
                vertex.setLeftUpper(upper.get(0));
            }
            List<Coord> lower = coords.stream().filter(c -> c.getY() < sy).collect(Collectors.toList());
            if (lower.size() != 2) {
                return null;
            }
            if (lower.get(0).getX() < lower.get(1).getX()) {
                vertex.setRightLower(lower.get(0));
                vertex.setLeftLower(lower.get(1));
            } else {
                vertex.setRightLower(lower.get(1));
                vertex.setLeftLower(lower.get(0));
            }
        }

        int upperY = -safeDistance;
        int lowerY = safeDistance;
        int rightX = safeDistance;
        int leftX = -safeDistance;
        Coord leftLower = vertex.getLeftLower();
        leftLower.setX(leftLower.getX() + leftX);
        leftLower.setY(leftLower.getY() + lowerY);
        //
        Coord rightLower = vertex.getRightLower();
        rightLower.setX(rightLower.getX() + rightX);
        rightLower.setY(rightLower.getY() + lowerY);
        //
        Coord leftUpper = vertex.getLeftUpper();
        leftUpper.setX(leftUpper.getX() + leftX);
        leftUpper.setY(leftUpper.getY() + upperY);
        //
        Coord rightUpper = vertex.getRightUpper();
        rightUpper.setX(rightUpper.getX() + rightX);
        rightUpper.setY(rightUpper.getY() + upperY);
        return vertex;
    }
//
//    public static QuadrilateralVertex changeVertex(Coord leftTop, Coord rightLower, Integer safeDistance) {
//        QuadrilateralVertex vertex = new QuadrilateralVertex();
//        vertex.setSafeDistance(safeDistance);
//        vertex.setLeftUpper(leftTop);
//        vertex.setRightUpper(new Coord(rightLower.getX(), leftTop.getY(), 0));
//        vertex.setLeftLower(new Coord(leftTop.getX(), rightLower.getY(), 0));
//        vertex.setRightLower(rightLower);
//        return vertex;
//    }
//
//    /**
//     * 对角线转换成 中心点
//     *
//     * @return
//     */
//    public static QuadrilateralVertex changeVertex(Coord center, Integer x, Integer y, Integer safeDistance) {
//        QuadrilateralVertex vertex = new QuadrilateralVertex();
//        vertex.setSafeDistance(safeDistance);
//        vertex.setLeftUpper(new Coord(center.getX() + x, center.getY() + y, 0));
//        vertex.setRightUpper(new Coord(center.getX() - x, center.getY() + y, 0));
//        vertex.setLeftLower(new Coord(center.getX() + x, center.getY() - y, 0));
//        vertex.setRightLower(new Coord(center.getX() - x, center.getY() - y, 0));
//        return vertex;
//    }
//

    /**
     * 点是否在范围内
     *
     * @param vertex
     * @param point
     * @return
     */
    public static boolean pointInsideRectangle(QuadrilateralVertex vertex, Coord point) {
        return pointInsideRectangle(vertex.getLeftUpper(), vertex.getLeftLower(), vertex.getRightUpper(), vertex.getRightLower(), point);
    }
//

    /**
     * 点是否在范围内
     *
     * @param point
     * @return
     */
    public static boolean pointInsideRectangle(Coord leftUpper, Coord leftLower, Coord rightUpper, Coord rightLower, Coord point) {
        try {
            //线左上右上
            boolean b = isThePointCrossing(leftUpper, rightUpper, point, "<=", 1);
            if (!b) {
                return false;
            }
            //线右上右下
            b = isThePointCrossing(rightUpper, rightLower, point, ">=", 3);
            if (!b) {
                return false;
            }
            //左下右下
            b = isThePointCrossing(leftLower, rightLower, point, ">=", 2);
            if (!b) {
                return false;
            }
            //左上左下
            b = isThePointCrossing(leftUpper, leftLower, point, "<=", 4);
            return b;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
//

    /**
     * 点是否过线
     *
     * @param lc1       线上的第一个点
     * @param lc2       线上的第二个点
     * @param point     需要校验的点
     * @param type      线条类型（1：上，2：下，3：右，4：左）
     * @param condition 条件符号 ("<"：点是否在线的左边  “<=”：点在线上或左边  “=”：点在线上  “>=”：点在线上或右边  “>”：点在线的右边)
     * @return
     */
    public static boolean isThePointCrossing(Coord lc1, Coord lc2, Coord point, String condition, Integer type) {
        if (lc1.getX() == lc2.getX() && lc1.getY() == lc2.getY()) {
            throw new CustomException(-1, "XY平面上的点位置不能相同");
        }
        //检验标准值
        Double check = (double) (0);
        //待检验的值
        Double withCheck = (double) (0);
        Double k = 0.0;
        // x = k
        if (lc1.getX() == lc2.getX()) {
            check = (double) lc1.getX();
            withCheck = (double) (point.getX());
        } else if (lc1.getY() == lc2.getY()) {
            check = (double) (lc1.getY());
            withCheck = (double) (point.getY());
        } else {
            Double x1 = (double) (lc1.getX());
            Double x2 = (double) (lc2.getX());
            Double y1 = (double) (lc1.getY());
            Double y2 = (double) (lc2.getY());
            k = (y2 - y1) / (x2 - x1);
            Double b = y1 - k * x1;
            check = k * point.getX() + b;
            withCheck = (double) (point.getY());
        }
        if (k > 0 && type > 2) {
            if (condition.startsWith("<")) {
                condition = condition.replace("<", ">");
            } else {
                condition = condition.replace(">", "<");
            }
        }
        switch (condition) {
            case "<":
                return withCheck.compareTo(check) == -1;
            case "<=":
                return withCheck.compareTo(check) < 1;
            case "=":
                return withCheck.compareTo(check) == 0;
            case ">=":
                return withCheck.compareTo(check) > -1;
            case ">":
                return withCheck.compareTo(check) == 1;
            default:
                throw new CustomException(-1, "输入的条件不对");
        }
    }
//
//    /**
//     * 获取最高点坐标（Z值越小，堆放越高）
//     *
//     * @param coordList
//     * @return
//     */
//    public static Coord highestCoordinate(List<Coord> coordList) {
//        if (EmptyUtil.isEmpty(coordList)) {
//            return null;
//        }
//        List<Coord> region = new ArrayList<>();
//        region.addAll(coordList);
//        region.sort(new Comparator<Coord>() {
//            @Override
//            public int compare(Coord o1, Coord o2) {
//                return o1.getZ() - o2.getZ();
//            }
//        });
//        removeShadows(region);
//        Coord coord = new Coord(region.get(0).getX(), region.get(0).getY(), region.get(0).getZ());
//        return coord;
//    }
//
//
//    /**
//     * 获取最低点坐标（Z值越大，堆放越低）
//     *
//     * @param coordList
//     * @return
//     */
//    public static Coord lowestCoordinate(List<Coord> coordList) {
//        if (EmptyUtil.isEmpty(coordList)) {
//            return null;
//        }
//        coordList.sort(new Comparator<Coord>() {
//            @Override
//            public int compare(Coord o1, Coord o2) {
//                return o2.getZ() - o1.getZ();
//            }
//        });
//        Coord coord = new Coord(coordList.get(0).getX(), coordList.get(0).getY(), coordList.get(0).getZ());
//        return coord;
//    }
//

    /**
     * 分层
     *
     * @param rangeList   范围内DEM
     * @param floorHeight 层高
     * @param type        true：由低到高，false：由高到低
     * @return
     */
    public static List<List<Coord>> layers(List<Coord> rangeList, int floorHeight, boolean type) {
        if (floorHeight <= 0) {
            throw new CustomException(ExceptionEnum.COORD_LAYER_HEIGHT_ERROR);
        }
        List<List<Coord>> layers = new ArrayList<>();
        if (type) {
            int maxHeight = rangeList.stream().min(new CoordSort(4)).orElse(new Coord()).getZ();
            while (rangeList.size() != 0) {
                maxHeight = maxHeight + floorHeight;
                Iterator<Coord> iterator = rangeList.iterator();
                List<Coord> layer = new ArrayList<>();
                while (iterator.hasNext()) {
                    Coord range = iterator.next();
                    if (range.getZ() <= maxHeight) {
                        layer.add(range);
                        iterator.remove();
                    }
                }
                layers.add(layer);
            }
            return layers;
        } else {
            int minHeight = rangeList.stream().max(new CoordSort(4)).orElse(new Coord()).getZ();
            while (rangeList.size() != 0) {
                minHeight = minHeight - floorHeight;
                Iterator<Coord> iterator = rangeList.iterator();
                List<Coord> layer = new ArrayList<>();
                while (iterator.hasNext()) {
                    Coord range = iterator.next();
                    if (range.getZ() >= minHeight) {
                        layer.add(range);
                        iterator.remove();
                    }
                }
                layers.add(layer);
            }
            return layers;
        }
    }

    public static Coord centerCorrection(List<Coord> screens, QuadrilateralVertex range, int x, int y) {
        int max = screens.stream().max(new CoordSort(2)).get().getX();
        int mix = screens.stream().min(new CoordSort(2)).get().getX();
        int may = screens.stream().max(new CoordSort(3)).get().getY();
        int miy = screens.stream().min(new CoordSort(3)).get().getY();
        int miz = screens.stream().min(new CoordSort(4)).get().getZ();
        Coord center = new Coord((max + mix) / 2, (may + miy) / 2, miz);
        if (center.getX() + x > range.getMaxX()) {
            center.setX(range.getMaxX() - x);
        }
        if (center.getX() - x < range.getMinX()) {
            center.setX(range.getMinX() + x);
        }
        if (center.getY() + y > range.getMaxY()) {
            center.setY(range.getMaxY() - y);
        }
        if (center.getY() - y < range.getMinY()) {
            center.setY(range.getMinY() + y);
        }
        return center;
    }
//
//    public static List<BWhlCoordinates> split25Pieces(List<Coord> screens, List<BWhlCoordinates> coordinates, BWhl whl) {
//        // 获取库区四个顶点
//        List<Coord> areas = new ArrayList<>();
//        areas.add(whl.getTopLeftCorner());
//        areas.add(whl.getLowerRightCorner());
//        areas.add(new Coord(whl.getTopLeftCorner().getX(), whl.getLowerRightCorner().getY(), 0));
//        areas.add(new Coord(whl.getLowerRightCorner().getX(), whl.getTopLeftCorner().getY(), 0));
//        QuadrilateralVertex vertex = CoordinatesUtils.changeVertex(areas, CoordConstant.Boundary_Safe_Distance);
//        // 筛选库区
//        List<Coord> region = CoordinatesUtils.screenDesignationRegion(vertex, screens);
//        int incrForX = (vertex.getMaxX() - vertex.getMinX()) / 5;
//        int incrForY = (vertex.getMaxY() - vertex.getMinY()) / 5;
//        if (coordinates == null) {
//            coordinates = new ArrayList<>();
//        }
//        Map<Integer, BWhlCoordinates> map = coordinates.stream().collect(Collectors.toMap(o -> o.getCoordinatesValue(), o -> o));
//        int index = 0;
//        for (int y = vertex.getMinY(); y < vertex.getMaxY(); y += incrForY) {
//            for (int x = vertex.getMinX(); x < vertex.getMaxX(); x += incrForX) {
//                index++;
//                BWhlCoordinates c = map.get(index);
//                if (c == null) {
//                    c = new BWhlCoordinates();
//                    c.setBWhlCoordinatesOid(SnowflakeIdUtils.generate());
//                    c.setBWhlFk(whl.getBWhloid());
//                    coordinates.add(c);
//                    c.setCoordinatesValue(index);
//                }
//                Coord center = new Coord(x + incrForX / 2, y + incrForY / 2, 0);
//                c.setCoordinatesX(center.getX());
//                c.setCoordinatesY(center.getY());
//                QuadrilateralVertex sv = CoordinatesUtils.queryVertex(center, incrForX / 2, incrForY / 2);
//                List<Coord> sc = CoordinatesUtils.screenDesignationRegion(sv, region);
//                int h = sc.stream().min(new CoordSort(4)).orElse(new Coord()).getZ();
//                c.setCoordinatesZ(h);
//            }
//        }
//        return coordinates;
//    }
}
